package cn.broadsense.common.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * @author: pengxin
 * @create: 2025-03-04 14:50
 * @Version 1.0
 **/
public class RsaUtils {



    public static String publicKey= "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuTSGJ2Dn8zLSeTOeS2BO\n" +
            "ODnGFfAhLrFr3btgQsOBFpVwZ3uqrNC30+lwHhq7ZdnLgqXwSF9Uk8eVxw+V+y1h\n" +
            "CxxMzMoVoAt1KbepWKUqJ5YifoqOUbOuXbKeQNw+jOSTlnHv9mgtTTxpW+9NNfdU\n" +
            "0b5gKed+DO29I3c/sJIyB67slYzh5qYtjQwhB2aoMlH6m9FrT7abdUSWjlNOlTfQ\n" +
            "LWv9nK6hbVOhwOYHjEOpYyZbJOCLFv/cB57yaLQcYjLLvix/aaEWaNmKeN2KdZpW\n" +
            "n0drI43jKnMoKhvP2uZO03PnAe4D1RSa1HrrQyiiDc5VvXF+zcvnqgYL6AxRtlmw\n" +
            "QwIDAQAB";

    public static String privateKey =
            "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCZ1k0HVT3r2OiN" +
                    "DVRqg3hRSNbcvTOQ0rUyei5ivFISD18C5Q/LzmiqiNVnvXCfaRi/Hc4Xn0F6ymDv" +
                    "R5fxp1L4SLFqs9/RXHJwxFAWztLk2fiVkWRODjC3oFHvmpymgz0PDN6eSxvZ5e8W" +
                    "se2z1ZmLzVBVtn7tYNGombaHNs2x5B9Oq2bwliouVjpgYXCA8hLmrO5SYlt/R6dK" +
                    "bIMyrm34X7owrXlIlPPQ6HbMt20rxl/+58n5XA8N40eBQHq29/4718c5O11A2cjo" +
                    "r9FfX7AR2AMlj8ujCuyMztBNf2xugn1FIVeOgvMaBq3GPDhjDyDOq2vYapI4FxfQ" +
                    "jFu0AakNAgMBAAECggEATZ3sW2jMxzQ3kQNKtjsC97ASiGkFnWpSdddjk/mERwxu" +
                    "WZ2M7L2F3ZUQZzkTm7qA+9xj4VM+zF9TrSaoKQrNN+wlZ7q9kHpeWzsRVZCgo0q+" +
                    "u2gR/bf3KI7BCG0v4GuS4QcTQzaOJNB4YzD+x59by0eZ4sUjIU2aOsDjcTFbcwbn" +
                    "2coXN4LJZIPi+0+/TA5Ey4n+P1xP0vUAQwPX+BUUPdWBrxZjdEVOVHY3USK6Mg1X" +
                    "YVYO6Ya9A6FYWtejSqoMZkBI6lSApxvYkdR/RJMl+6y3jaJsytbK40htMlzsHfYm" +
                    "9o4leMLiaw8vk2kDFaQyA1DooO5IUrvq89EJk3heQQKBgQDLxMiNWQvy9ucKAPZF" +
                    "jvTdf8KkW8gcKfv5qfSSc/j3++67pAgyj1jnQefsEA49iCIBmutkaqLnZ5ccS2xy" +
                    "xT1rArre2OfN0gNCcpO1Mot2mfmhYD2j3hUZh3z2OQ87KkhEihmkHtgItKxG6T9F" +
                    "+94KrVvQNWWVFvbWpJsvvS9sKQKBgQDBRQdGxiWza6sGE354ySkhccN0zCvp1c7V" +
                    "1tEy+KxqZZivHGZz5ryYFp+0oG7Q3rfCYdaFPcpGtUCiGKvtC79SDD/8YIg+z1Dq" +
                    "KqGy959DpoRg2sBOeFUN3C7+RpcxgeYat324ncrRwtGYGSuoqZAB1YoraJ8fxm7q" +
                    "qeABJX2yRQKBgFANkEjPqf0a+OuaMyZiolp+veY6VKST7fVIytfajf5G0KSRLdNH" +
                    "M8MuxkGckSulVs7QoRfDORq2PMyV6RYx3Q40dVvlVTQsBLHPN65hHB9NIKBAohbF" +
                    "wf5Ul0hYZF53HwsmQiqTH4ZUDLaTHhONfs8tCjwDLlRjQDhVVM5tgbr5AoGAUysL" +
                    "SI94Jc+NVx4IbJ3UD0pPW/GIbCXPzgGVIKQWPereW2yTQgjm22PQ8f+f8E4HMaS8" +
                    "lbXTV74jxB1tzqIhgIM4/bfkqXK5VStnTgZ1CL2ddbhszNfHciFwL4fdBDbr+E9B" +
                    "0LJGVyHI4/Tg7sGY8NEdljR9VUUk8eKMPLIPdykCgYEAq0MwLPMtimytGeuYdrRJ" +
                    "jkVqePImLu6iGVQSrAsOcPj+t2rquXS5sQIpBnl0LYrXfIO47Aiy66LTiQzAO/C6" +
                    "uLZN6DmoNq5q0mlaX6PsYRgtcr1F84Ofe6yz+GFXspxpL5rIJiOkVDIso7FtJ29Z" +
                    "VxRbHPGwlrekdxDeLGvm+Qk=" ;

    /**
     * 私钥解密
     *
     * @param text             待解密的文本
     * @return 解密后的文本
     */
    public static String decryptByPrivateKey(String text) throws Exception {
        return decryptByPrivateKey(privateKey, text);
    }

    /**
     * 公钥解密
     *
     * @param publicKeyString 公钥
     * @param text            待解密的信息
     * @return 解密后的文本
     */
    public static String decryptByPublicKey(String publicKeyString, String text) throws Exception {
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyString));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        byte[] result = cipher.doFinal(Base64.decodeBase64(text));
        return new String(result);
    }

    /**
     * 私钥加密
     *
     * @param privateKeyString 私钥
     * @param text             待加密的信息
     * @return 加密后的文本
     */
    public static String encryptByPrivateKey(String privateKeyString, String text) throws Exception {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        byte[] result = cipher.doFinal(text.getBytes());
        return Base64.encodeBase64String(result);
    }

    /**
     * 私钥解密
     *
     * @param privateKeyString 私钥
     * @param text             待解密的文本
     * @return 解密后的文本
     */
    public static String decryptByPrivateKey(String privateKeyString, String text) throws Exception {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] result = cipher.doFinal(Base64.decodeBase64(text));
        return new String(result);
    }

    /**
     * 公钥加密
     *
     * @param publicKeyString 公钥
     * @param text            待加密的文本
     * @return 加密后的文本
     */
    public static String encryptByPublicKey(String publicKeyString, String text) throws Exception {
        X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyString));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] result = cipher.doFinal(text.getBytes());
        return Base64.encodeBase64String(result);
    }

    /**
     * 构建RSA密钥对
     *
     * @return 生成后的公私钥信息
     */
    public static RsaKeyPair generateKeyPair() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(1024);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
        String publicKeyString = Base64.encodeBase64String(rsaPublicKey.getEncoded());
        String privateKeyString = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
        return new RsaKeyPair(publicKeyString, privateKeyString);
    }

    /**
     * RSA密钥对对象
     */
    public static class RsaKeyPair {
        private final String publicKey;
        private final String privateKey;

        public RsaKeyPair(String publicKey, String privateKey) {
            this.publicKey = publicKey;
            this.privateKey = privateKey;
        }

        public String getPublicKey() {
            return publicKey;
        }

        public String getPrivateKey() {
            return privateKey;
        }

    }
}